home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / contrib / dvx / docs / daemons.doc < prev    next >
Encoding:
Text File  |  1993-07-15  |  7.0 KB  |  237 lines

  1.  
  2. Network Daemons Programming Reference
  3. -------------------------------------
  4. In order for the DESQview/X Network Manager to process certain 
  5. service requests, a daemon (that is, unseen) process may be 
  6. started to handle the request. This section describes the 
  7. programming interface provided for developers to create these 
  8. network daemons.
  9.  
  10. To configure the DESQview/X Network Manager to use a new daemon, 
  11. see the Network Manager Configuration section for details.
  12.  
  13. Note that network daemons use the Berkeley Socket Interface to 
  14. communicate with the DESQview/X Network Manager (see that section 
  15. for details). In addition, DESQview/X supplies extension routines 
  16. to facilitate the creation of new daemons.
  17.  
  18.  
  19. Daemon Types
  20. ------------
  21. There are two type of daemons that may be created for use in a 
  22. DESQview/X system - regular and lingering daemons:
  23.  
  24. Regular       These kinds of daemons are started by the Network 
  25.               Manager, process the supplied request and then 
  26.               terminate immediately.
  27.  
  28. Lingering     A lingering daemon is one that also processes the 
  29.               supplied request, but instead of terminating 
  30.               immediately,"lingers" for a specified period of 
  31.               time in anticipation of another service request.  
  32.               If no service request is accepted by the Network 
  33.               Manager before the linger time expires, the daemon 
  34.               is terminated.
  35.  
  36. Note that each daemon type may appear in one of two flavors - the 
  37. daemon may be a TCP (stream) daemon or a UDP (datagram) daemon.
  38.  
  39.  
  40. General Operation
  41. -----------------
  42. The interaction between the Network Manager and a network daemon 
  43. is as follows:
  44.  
  45.     The Network Manager accepts a request for a particular 
  46.     service that has a daemon associated with it.
  47.  
  48.     If the daemon is not currently running, the Network Manager 
  49.     instructs DESQview/X to start the daemon and passes the 
  50.     service request to it.
  51.  
  52.     For daemons already running, the Network Manager forwards the 
  53.     service request directly to the daemon.
  54.  
  55.     When the daemon has processed the request, it either 
  56.     terminates or lingers for a specified period of time.
  57.  
  58.  
  59. Anatomy of a Daemon
  60. -------------------
  61. In order to facilitate the communication between the DESQview/X 
  62. Network Manager and a daemon, DESQview/X supplies extension 
  63. routines (listed at the end of this section). The most important 
  64. of these routines is the so_daemon call - this encapsulates a lot 
  65. of handshaking and passing of parameters between the Network 
  66. Manager and the daemon and frees the daemon writer from the 
  67. tedium of performing a lot of work.
  68.  
  69. A regular daemon will normally use the so_daemon and 
  70. so_exitdaemon routines by following this model:
  71.  
  72. #include <netdb.h>
  73. #include <netinet\in.h>
  74. #include <sys\socket.h>
  75.  
  76. void main(int argc,char *argv[])
  77. {
  78.   int s;
  79.  
  80.    /* ... main work ... */
  81.  
  82.   if((s = so_daemon(argv,FALSE)) >= 0) {
  83.      perform_bsd_socket_work();
  84.      so_close(s);  /* for TCP daemons ... or ... */
  85.      so_unlink(s); /* for UDP daemons */
  86.      }
  87.   so_exitdaemon();
  88. }
  89.  
  90. A lingering daemon will normally use the so_daemon and 
  91. so_exitdaemon routines by following this model:
  92.  
  93. #include <netdb.h>
  94. #include <netinet\in.h>
  95. #include <sys\socket.h>
  96.  
  97. void main(int argc,char *argv[])
  98. {
  99.   int s;
  100.  
  101.   /* ... main loop ... */
  102.  
  103.   while (1) {
  104.      if((s = so_daemon(argv,TRUE)) <0)
  105.         continue;
  106.      perform_bsd_socket_work();
  107.      so_close(s);  /* for TCP daemons ... or ... */
  108.      so_unlink(s); /* for UDP daemons */
  109.      }
  110.  }
  111.  
  112. Note that the so_daemon routine handles the linger time for the 
  113. daemon and will terminate the daemon if it is exceeded.
  114.  
  115. Differences between TCP and UDP Daemons
  116. ---------------------------------------
  117. There are several differences between a TCP and a UDP daemon that 
  118. should be noted.
  119.  
  120.     A TCP daemon will use the so_close routine to close a socket.
  121.  
  122.     A UDP daemon must use the so_unlink extension routine to 
  123.     release a socket. This is because UDP daemons are handed a 
  124.     child socket that was generated when a request for the 
  125.     service was made. Closing the socket with so_close would 
  126.     prevent further requests for that service being processed by 
  127.     the Network Manager.
  128.  
  129.     A TCP daemon will use TCP I/O primitives, such as recv and 
  130.     send in the main body of the daemon (perform_bsd_socket_work) 
  131.     to perform the required communication.
  132.  
  133.     A UDP daemon will use UDP I/O primitives, such as recvfrom 
  134.     and sendto in the main body of the daemon 
  135.     (perform_bsd_socket_work) to perform the required 
  136.     communication.
  137.  
  138. Daemon Routines
  139. ---------------
  140. The DESQview/X extension routines that enable network daemons to 
  141. be created with a minimum of effort are now listed in reference 
  142. form:
  143.  
  144.  
  145. so_daemon()
  146. ***********
  147.  
  148. Synopsis
  149. --------
  150. #include <sys\socket.h>
  151.  
  152. int so_daemon(char *args[],char wait);
  153.  
  154. Description
  155. -----------
  156. The so_daemon call is the linkup call for DESQview/X daemon 
  157. processes. The call retrieves the connection information for the 
  158. next connection to be processed by the daemon. The args parameter 
  159. must be the arguments passed to the application upon entry by the 
  160. C runtime library. The wait parameter allows the daemon to 
  161. optionally poll for additional connections or datagrams, while 
  162. continuing to process current connections. 
  163.  
  164. As implied above,  the call can be made for daemons of both type 
  165. SOCK_STREAM and SOCK_DGRAM. For SOCK_STREAM daemons, the 
  166. descriptor returned can be processed normally and closed with a 
  167. standard so_close call.  For SOCK_DGRAM daemons, the descriptor 
  168. is a datagram descriptor ready to make a recvfrom call. The 
  169. descriptor should then be closed via the so_unlink call. 
  170.  
  171. When the wait parameter is positive, the so_daemon 
  172. call will perform all of the duties of determining the proper linger 
  173. period and exiting when the time has been exceeded before the next 
  174. request arrives.
  175.  
  176. Notes
  177. -----
  178. DESQview/X extension call.
  179.  
  180. Return Values
  181. -------------
  182. so_daemon returns a non-negative descriptor on success and -1 on 
  183. failure.
  184.  
  185.  
  186. so_exitdaemon()
  187. ***************
  188.  
  189. Synopsis
  190. --------
  191. #include <sys\socket.h>
  192.  
  193. void so_exitdaemon(void);
  194.  
  195. Description
  196. -----------
  197. The so_exitdaemon call should be made before the daemon process 
  198. terminates. This will allow the socket interface to be 
  199. uninitialized correctly.
  200.  
  201. Notes
  202. -----
  203. DESQview/X extension call.
  204.  
  205.  
  206. so_unlink()
  207. ***********
  208.  
  209. Synopsis
  210. --------
  211. #include <sys\socket.h>
  212.  
  213. int so_unlink(int s);
  214.  
  215. Description
  216. -----------
  217. The so_unlink call returns ownership of a daemon socket to the 
  218. DESQview/X Network Manager. This call is provided for SOCK_DGRAM 
  219. type sockets so as to allow them to continue pending after 
  220. termination of a daemon process. This call must be used in place 
  221. of the so_close call for datagram daemons.
  222.  
  223. Notes
  224. -----
  225. DESQview/X extension call.
  226.  
  227. Return Values
  228. -------------
  229. so_unlink returns 0 on success, -1  on failure (errno as 
  230. described below).
  231.  
  232. Errors
  233. ------
  234. ENOTSOCK      Invalid descriptor.
  235.  
  236.  
  237.